package org.hamster.multidropbox.main.services
{
	import flash.display.DisplayObject;
	
	import mx.controls.Text;
	import mx.managers.PopUpManager;
	
	import org.hamster.multidropbox.main.events.AppTipEvent;
	import org.hamster.multidropbox.main.view.AppTip;

	public class AppTipService
	{
		private static var _instance:AppTipService;
		
		public static function get instance():AppTipService
		{
			if (!_instance) {
				_instance = new AppTipService();
			}
			return _instance;
		}
		
		/**
		 * @param place lefttoprightbottom
		 * 
		 */
		public static function register(app:DisplayObject, left:int = -1, right:int = -1, top:int = -1, bottom:int = -1):void
		{
			instance.app     = app;
			instance.left    = left;
			instance.right   = right;
			instance.top     = top;
			instance.bottom  = bottom;
		}
		
		public function AppTipService()
		{
			appTip = new AppTip();
			appTip.addEventListener(AppTipEvent.CLOSE_WINDOW, closeWindowHandler);
		}
		
		public var appTip:AppTip;
		public var app:DisplayObject;
		
		public var left:int;
		public var right:int;
		public var top:int;
		public var bottom:int;
		
		private var isPopUp:Boolean = false;
		
		
		public function error(title:String, text:String):void
		{
			this.show(title, text, 0xff95a1);
		}
		
		public function show(title:String, text:String, color:int = -1):void
		{
			if (!isPopUp) {
				PopUpManager.addPopUp(appTip, app, false);
				if (left != -1) {
					appTip.x = left;
				}
				if (right != -1) {
					appTip.x = app.width - appTip.width - right;
				}
				if (top != -1) {
					appTip.y = top;
				}
				if (bottom != -1) {
					appTip.y = app.height - appTip.height - top;
				}
				isPopUp = true;
			}
			appTip.show(title, text, color);
		}
		
		private function closeWindowHandler(event:AppTipEvent):void
		{
			isPopUp = false;
			PopUpManager.removePopUp(appTip);
		}
	}
}